//运维详情（整车）

interface IOperationDetailViewModel extends ng.IScope {
    /**
     * 时间日期格式
     */
    dateFormat: "{format:'YYYY.MM.DD HH:mm'}";
    /**
     * 修改承运商信息按钮是否显示
     */
    carryOrderIsShow: boolean;
    /**
     * 修改车辆信息按钮是否显示
     */
    carInfoIsShow: boolean;
    /**
     * 修改应付价格信息按钮是否显示
     */
    receivableIsShow: boolean;
    /**
     * 修改合同信息按钮启用禁用
     */
    contractDIs: boolean;
    /**
     * 修改合同信息按钮是否显示
     */
    contractIsShow: boolean;
    /**
     * 修改附件按钮是否显示
     */
    attachmentIsShow: boolean;
    /**
     * 回单附件启用禁用
     */
    receiptDis: boolean;
    /**
     * 开户银行启用禁用
     */
    openBankDis: boolean;
    /**
     * 承运商类型下拉
     */
    carriertypeList: ValueListItemResponse[];
    /**
     * 应付单价单位下拉
     */
    receivablePriceUnitDropDown: ValueListItemResponse[];
    /**
     * 预付款单位下拉
     */
    advancePriceTypeDropDown: ValueListItemResponse[];
    /**
     * 合同货物数量单位下拉
     */
    goodsNumUnitList: ValueListItemResponse[];
    /**
     * 油卡状态下拉
     */
    oilCardStatusList: ValueListItemResponse[];
    /**
     * 承运单信息
     */
    CData: invoiceManagement;
    /**
     * 订单信息
     */
    OData: invoiceManagementOrder;
    /**
     * 总线路列表
     */
    orderTotalList: invoiceManagementOrder[];
    /**
     * 子线路列表
     */
    orderChildrenList: invoiceManagementOrderChild[];
    /**
     * 总线路查看详情
     */
    getOrderDetial: (data: invoiceManagementOrder) => void;
    /**
     * 总线路详情数据
     */
    orderDetial: invoiceManagementOrder;
    /**
     * 子线路查看详情
     */
    EditChildren: (data: invoiceManagementOrderChild) => void;
    /**
     * 子线路详情数据
     */
    orderChil: invoiceManagementOrderChild;
    /**
     * 订单附件上传组件
     */
    attachmentUploader: any;
    /**
     * app竞价列表
     */
    orderAppBiddingList: quotationResponse[];
    /**
     * 合同信息
     */
    contractData: invoiceManagementContract;
    /**
     * 付款信息列表
     */
    orderCostList: invoiceManagementFee[];
    /**
     * 修改承运商信息
     */
    EditCarryOrder: () => void;
    /**
     * 承运商号码
     */
    carrierPhoneBox: string;
    /**
     * 承运商类型
     */
    carrierTypeBox: string;
    /**
     * 保存承运商信息
     */
    saveCarryOrder: () => void;
    /**
     * 车长下拉
     */
    carLengthList: ValueListItemResponse[];
    /**
     * 车型下拉
     */
    carTypeList: ValueListItemResponse[];
    /**
     * 车辆
     */
    car: string;
    /**
     * 车辆id
     */
    carId: string;
    /**
     * 修改车辆信息
     */
    editCarInfo: () => void;
    /**
     * 弹框车辆Id
     */
    carIdBox: string;
    /**
     * 车牌号
     */
    carCodeBox: string;
    /**
     * 驾驶员姓名
     */
    driverNameBox: string;
    /**
     * 驾驶员号码
     */
    driverPhoneBox: string;
    /**
     * 车型
     */
    carTypeBox: string;
    /**
     * 车长
     */
    carLengthBox: string;
     /**
     * 所属辖区市列表
     */
    deliverCityList: any;
    /**
     * 所属辖区区列表
     */
    deliverAreaList: any;
     /** 
     * 道路运输许可证号
    */
    permitNumberBox: string;
     /** 
     * 道路运输证字号
    */
    roadTransportCertificateNumberBox: string;
    /**
     * 所属辖区
     */
    countrySubdivisionValue: string;
    /** 
     * 所属辖区
    */
    countrySubdivisionProvince: string;
    /** 
     * 所属辖区
    */
    countrySubdivisionCity: string;
    /** 
     * 所属辖区
    */
    countrySubdivisionCountry: string;
    /** 
     * 道路运输许可证号
    */
    permitNumber: string;
    /** 
     * 所属辖区(省)
    */
    countrySubdivisionProvinceCode: string;
    /** 
     * 所属辖区(市)
    */
    countrySubdivisionCityCode: string;
    /** 
     * 所属辖区(区)
    */
    countrySubdivisionCountryCode: string;
    /** 
     * 道路运输证字号
    */
    roadTransportCertificateNumber: string;
    /**
    * 所属辖区code
    */
    countrySubdivisionCode: string;
    /**
     * 保存车辆信息
     */
    saveCarInfo: () => void;
    /**
     * 修改应付价格
     */
    editReceivable: () => void;
    /**
     * 应付单价
     */
    payablePriceBox: number;
    /**
     * 应付单价单位
     */
    payablePriceUnitBox: string;
    /**
     * 应付说明
     */
    payableSummaryBox: string;
    /**
     * 预付款1
     */
    advancePriceBox: string;
    /**
     * 预付款1单位
     */
    advancePriceTypeBox: string;
    /**
     * 预付款2
     */
    advancePriceTwoBox: string;
    /**
     * 预付款2单位
     */
    advancePriceTypeTwoBox: string;
    /**
     * 回单款
     */
    receiptPriceBox: number;
    /**
     * 回单款单位
     */
    receiptPriceUnitBox: string;
    /**
     * 银行卡号
     */
    bankCode: string;
    /**
     * 开户银行
     */
    bankNameBox: string;
    /**
     * 持卡人姓名
     */
    holderBox: string;
    /**
     * 保存应付价格按钮防并发
     */
    //saveReceivableDis:boolean;
    /**
     * 保存应付价格信息
     */
    saveReceivable: () => void;
    /**
     * 修改合同信息
     */
    editContract: () => void;
    /**
     * 合同号
     */
    contractNumberBox: string;
    /**
     * 合同货物数量
     */
    quantityOfGoodsBox: number;
    /**
     * 合同货物数量单位
     */
    goodsUnitBox: string;
    /**
     * 油卡卡号
     */
    oilCardNumberBox: string;
    /**
     * 油卡状态
     */
    oilCardStatusBox: string;
    /**
     * 驾驶证号
     */
    driverLicenseNumberBox: string;
    /**
     * 车架号
     */
    frameNumberBox: string;
    /**
     * 挂车号
     */
    trailerNumberBox: string;
    /**
     * 发动机号
     */
    engineNumberBox: string;
    /**
     * 备用电话
     */
    altermatePhoneBox: string;
    /**
     * 辅助工具
     */
    auxiliaryToolBox: string;
    /**
     * 挂车车架号
     */
    trailerFrameNumberBox: string;
    /**
     * 运费总价
     */
    totalPriceBox:number
    /**
     * 保存合同信息
     */
    saveContract: () => void;
    /**
    * 驾驶证上传组件
    */
    driverUploader: any;
    /**
     * 挂车证上传组件
     */
    trailerUploader: any;
    /**
     * 行驶证上传组件
     */
    travelUploader: any;
    /**
     * 身份证上传组件
     */
    idCardUploader: any;
    /**
     * 车辆照片
     */
    carPicUploader: any;
      /**
     * 道路运输证
     */
    roadTransportCardUploader:any
    /**
     * 道路运输经营许可证
     */
    roadTransportLicenseUploader:any;
    /**
     * 从业资格证
     */
    occupationalLicenseUploader:any;
    /**
     * 磅单照片
     */
    weightNoteUploader:any;
    /**
     * 修改附件
     */
    EditAttachment: () => void;
    /**
     * 驾驶证
     */
    driverUploaderBox: any;
    /**
     * 挂车证
     */
    trailerUploaderBox: any;
    /**
     * 行驶证
     */
    travelUploaderBox: any;
    /**
     * 身份证
     */
    idCardUploaderBox: any;
    /**
     * 车辆照片
     */
    carPicUploaderBox: any;
    /**
     * 道路运输证
     */
    roadTransportCardUploaderBox:any
    /**
     * 道路运输经营许可证
     */
    roadTransportLicenseUploaderBox:any;
    /**
     * 从业资格证
     */
    occupationalLicenseUploaderBox:any;
    /**
     * 磅单照片
     */
    weightNoteUploaderBox:any
    /**
     * 保存附件信息
     */
    saveAttachment: () => void;
    /**
     * 取消修改附件
     */
    cancelSaveAttachment: () => void;
    /**
     * 回单附件
     */
    childOrderAttachment: any;
    /**
     * 承运商列表自动填充
     */
    searchCarrier(value: string): any;
    /**
     * 选中承运商自动填充数据
     */
    carrierSelect(select: angucomplete<CarrierListItemResponse>): void;

    /**
     * 车辆列表自动填充
     */
    searchCar(value: string): any;
    /**
     * 选中车牌号自动填充数据
     */
    carCodeSelect(select: angucomplete<CarListItemResponse>): void;
    /**
     * 银行卡列表自动填充
     */
    searchBank(value: string): any;
    /**
     * 选中银行卡自动填充数据
     */
    bankCodeSelect(select: angucomplete<BankListItemResponse>): void;
    /**
     * 获取银行卡名称
     */
    getBankName: () => void;
    /**
     * 返回事件
     */
    backUrl: () => void;
     /**
     * 点击图片时候触发，用以旋转放大图片
     */
    handleImgClick:(item:any)=>void;
    /**
     * 用以旋转放大的图片的路径
     */
    selectedImgSrc:string;

    //承运单备注
    remark:string;
    modalRemark:string;
    
    attachList:OssUploaderItem[];
    attachList2:OssUploaderItem[];
}

class OperationDetailController {
    uploaderList: Array<AddCarAttachmentDetail>;
    attachList: Array<AddCarAttachmentDetail>;
    baseImageUrl: string;
    /**
     * 预付款
     */
    AdvancePaymentList: advancePaymentList[] = [];
    /**
     * 承运单id
     */
    guid: string;
    /**返回值 */
extData:any;

    constructor(private $scope: IOperationDetailViewModel, private orderService: IorderService, private $location: ng.ILocationService,  private routeService: routeService, private $ngBootbox: BootboxService, private $http: ng.IHttpService,
        private operationService: IOperationService, private authorizationService: IAuthorizationService, private feeService: IFeeService, private goodsTypeService: IGoodsTypeService, private goodsService: IGoodsService,
        private valueService: IValueService, private $state: angular.ui.IStateService, private carService: ICarService, private commonService: ICommonService, private $timeout: ng.ITimeoutService, private areaService: IAreaService,private ossUploaderService:ossUploaderService) {
        this.uploaderList = [];
        this.attachList = [];
        this.baseImageUrl = this.routeService.getBaseUrl() + "attachment/";
        this.init();
    }

    init(): void {
        this.guid = this.$location.search().id;

        this.$scope.contractDIs = true;
        this.$scope.carryOrderIsShow = false;
        this.$scope.carInfoIsShow = false;
        this.$scope.receivableIsShow = false;
        this.$scope.contractIsShow = false;
        this.$scope.attachmentIsShow = false;
        this.$scope.receiptDis = true;
        this.$scope.openBankDis = true;
        //this.$scope.saveReceivableDis = false;
        /**
         * 权限验证
         */
        this.authorizationService.auth("5E1F9989AA5F596D06429E0D93616A47ACE03F2B9F7C90D35FD5A057D3AF3782").then((res) => {
            this.$scope.carryOrderIsShow = res.data.extData;
        });

        this.authorizationService.auth("5E1F9989AA5F596D06429E0D93616A478B4FACDCAEB73F46").then((res) => {
            this.$scope.carInfoIsShow = res.data.extData;
        });

        this.authorizationService.auth("5E1F9989AA5F596D06429E0D93616A47A84CECFF3A17D6A862506D4A1091DAB9").then((res) => {
            this.$scope.receivableIsShow = res.data.extData;
        });
        this.authorizationService.auth("5E1F9989AA5F596D06429E0D93616A478CBCA692368F067B5CA4D24B245211A6").then((res) => {
            this.$scope.contractIsShow = res.data.extData;
        });
        this.authorizationService.auth("5E1F9989AA5F596D06429E0D93616A47340EFD81C08E9D28A3C18E92E2705152").then((res) => {
            this.$scope.attachmentIsShow = res.data.extData;
        });

        /**
         * 获取线路详情
         */
        this.$scope.getOrderDetial = this.getOrderDetial;
        this.$scope.EditChildren = this.EditChildren;
        /**
         * 修改信息
         */
        this.$scope.EditCarryOrder = this.EditCarryOrder;
        this.$scope.editCarInfo = this.editCarInfo;
        this.$scope.editReceivable = this.editReceivable;
        this.$scope.editContract = this.editContract;
        this.$scope.EditAttachment = this.EditAttachment;

        this.$scope.backUrl = this.backUrl;
          
        // 详情中的附件
        this.$scope.attachList=[
            {
                title:'驾驶证',
                uploader:null,
                uploaderName:'driverUploader'
            },{
                title:'挂车证',
                uploader:null,
                uploaderName:'trailerUploader'
            },{
                title:'行驶证',
                uploader:null,
                uploaderName:'travelUploader'
            },{
                title:'身份证',
                uploader:null,
                uploaderName:'idCardUploader'
            },{
                title:'车辆照片',
                uploader:null,
                uploaderName:'carPicUploader'
            },{
                title:'道路运输证',
                uploader:null,
                uploaderName:'roadTransportCardUploader'
            },{
                title:'道路运输经营许可证',
                uploader:null,
                uploaderName:'roadTransportLicenseUploader'
            },{
                title:'从业资格证',
                uploader:null,
                uploaderName:'occupationalLicenseUploader'
            },{
                title:'磅单照片',
                uploader:null,
                uploaderName:'weightNoteUploader'
            },
        ]
        const OssFile = this.ossUploaderService.getOssFile();
        this.$scope.attachList.forEach((attach,index)=>{
            attach.uploader=new OssFile(this.$scope,attach.uploader,{uploaderList:[],tag:index+1});
           
        })
        // 订单附加
        this.$scope.attachmentUploader=new OssFile(this.$scope,this.$scope.attachmentUploader,{uploaderList:[],tag:1});

        // 修改附件
        this.$scope.attachList2=[
            {
                title:'驾驶证',
                uploader:null,
                uploaderName:'driverUploader2'
            },{
                title:'挂车证',
                uploader:null,
                uploaderName:'trailerUploader2'
            },{
                title:'行驶证',
                uploader:null,
                uploaderName:'travelUploader2'
            },{
                title:'身份证',
                uploader:null,
                uploaderName:'idCardUploader2'
            },{
                title:'车辆照片',
                uploader:null,
                uploaderName:'carPicUploader2'
            },{
                title:'道路运输证',
                uploader:null,
                uploaderName:'roadTransportCardUploader2'
            },{
                title:'道路运输经营许可证',
                uploader:null,
                uploaderName:'roadTransportLicenseUploader2'
            },{
                title:'从业资格证',
                uploader:null,
                uploaderName:'occupationalLicenseUploader2'
            },{
                title:'磅单照片',
                uploader:null,
                uploaderName:'weightNoteUploader2'
            },
        ]

        this.$scope.attachList2.forEach((attach,index)=>{
            attach.uploader=new OssFile(this.$scope,attach.uploader,{uploaderList:this.uploaderList,tag:index+1});
            attach.uploader.onSuccess=(item)=>{this.onSuccessItem(item.fileName,item.imgSrc,index+1,item._file)};
            attach.uploader.onDelete=(item)=>{
                attach.uploader.queue=attach.uploader.queue.filter(it=>item.fileName!=it.fileName&&item.imgSrc!=it.imgSrc);
                this.uploaderList=this.uploaderList.filter(it=>attach.uploader.tag!=it.attachmentTag)
            }
        })



        this.loadData();

        $('#opencontractTime').click(() => {
            $('#contractTime').datetimepicker('show');
        });
        $('#openArrivalTime').click(() => {
            $('#arrivalTime').datetimepicker('show');
        });
        this.$scope.handleImgClick=(item)=>{this.$scope.selectedImgSrc=item.imgSrc;}
    }

    

    loadData = () => {
        /**
         * 获取承运单信息
         */
        this.orderService.invoiceManagementDetail(this.guid).then((res) => {
            this.$scope.CData = res.data;
            this.$scope.OData = res.data.orderList[0];
            this.$scope.orderTotalList = res.data.orderList;//总线路列表
            this.$scope.orderChildrenList = res.data.orderList[0].childList;//子线路列表
            //车辆
            this.$scope.carId = res.data.carId;
            this.operationService.getCarDetail(this.$scope.carId).then((result) => {
                let driver;
                if (result.data.driver == null) {
                    driver = "";
                }
                else {
                    driver = result.data.driver;
                }
                this.$scope.car = result.data.carCode + " " + driver + " " + result.data.driverPhone;

            });

            this.$scope.permitNumber = res.data.permitNumber;
            let countrySubdivisionValue;
            this.$scope.countrySubdivisionValue = res.data.countrySubdivisionProvince + res.data.countrySubdivisionCity + res.data.countrySubdivisionCountry
            this.$scope.roadTransportCertificateNumber = res.data.roadTransportCertificateNumber;

            //app竞价列表
            this.orderService.quotation(res.data.orderList[0].id).then(result => {
                this.$scope.orderAppBiddingList = result.data.data;
            });
            //合同信息
            this.$scope.contractData = res.data.contract;
            //12.1 新增 备注
            this.$scope.remark = res.data.remarks

            if (res.data.contract) {
                this.$scope.contractDIs = false;
            }
            this.$scope.orderCostList = res.data.feeList;//付款信息列表
            //附件列表
            this.uploaderList = [];
            this.attachList = [];
            this.$scope.attachmentUploader.clearQueue();
            this.$scope.OData.orderAttachments.forEach((item, index) => {
                this.uploaderList.push(item);
                this.attachList.push(item);
                this.commonService.showFileItem(item, this.$scope.attachmentUploader);
            });
            //承运单附件
            let carArr = this.$scope.CData.carrierOrderAttachments;
           
            this.$scope.attachList.forEach(attach=>{
                attach.uploader.clearQueue()
            })
            carArr.forEach((item, index) => {
                this.uploaderList.push(item);
                this.attachList.push(item);
                this.$scope.attachList[Number(item.attachmentTag)-1].uploader.addByAttach(item)
              
            });
            //回单附件
            //let childOrderAttachment = [];
            this.$scope.childOrderAttachment = [];
            const OssFile=this.ossUploaderService.getOssFile();

            this.$scope.orderChildrenList.forEach((item, index) => {

                this.$scope.childOrderAttachment.push(
                    {
                        title:'回单'+(index+1),
                        uploader:null,
                        uploaderName: 'receipt' + index+'Uploader'
                    }
                )

                this.$scope.childOrderAttachment[index].uploader=new OssFile(this.$scope,this.$scope.childOrderAttachment[index].uploader,{uploaderList:[],tag:index+1,multiple:true});


                let receiptUploaderArr = item.receiptAttachments;

                if (receiptUploaderArr.length > 0) {
                    for (let i = 0; i < receiptUploaderArr.length; i++) {
                        let receiptUploaderS = receiptUploaderArr[i];
                        this.$scope.childOrderAttachment[index].uploader.addByAttach(receiptUploaderS)
                    }
                }
            });
        });
    }

    /**
    * 获取市区下拉
    */
    getProvinceList = () => {
        this.areaService.getCity(this.$scope.countrySubdivisionProvinceCode).then(res => {
            this.$scope.deliverCityList = res.data.data;

        });
        this.areaService.getCounty(this.$scope.countrySubdivisionCityCode).then(res => {
            this.$scope.deliverAreaList = res.data.data;
        });
    }

    /**
     * 获取总线路详情
     */
    getOrderDetial = (data: invoiceManagementOrder) => {
        this.$scope.orderDetial = data;
    }

    /**
     * 获取子线路详情
     */
    EditChildren = (data: invoiceManagementOrderChild) => {
        this.$scope.orderChil = data;
    }

    /**
     * 修改承运商
     */
    EditCarryOrder = () => {
        $("#carrierName_value").val(this.$scope.CData.carrier);
        this.$scope.carrierPhoneBox = this.$scope.CData.carrierPhone;
        this.$scope.carrierTypeBox = this.$scope.CData.carrierTypeEnum;
        this.$scope.carriertypeList = this.valueService.getCarrierTypeList().data;
        this.$scope.searchCarrier = this.searchCarrier;
        this.$scope.carrierSelect = this.carrierSelect;
        this.$scope.saveCarryOrder = this.saveCarryOrder;
    }

    /**
     * 自动填充承运商信息
     */
    searchCarrier = (value: string) => {
        return this.operationService.getCarrierList("", null, value, "", "", "", "", "", true, 0, 5).then((res) => {
            return res.data.data;
        });
    }

    /**
     * 承运商自动填充信息
     */
    carrierSelect = (select: angucomplete<CarrierListItemResponse>) => {
        if (select) {
            this.$scope.CData.carrierId = select.description.id;
            this.$scope.carrierPhoneBox = select.description.phone;
            this.operationService.getCarrierDetail(select.description.id).then((res) => {
                this.$scope.carrierTypeBox = res.data.type;
            });
        }
    }

    /**
     * 保存修改承运商
     */
    saveCarryOrder = () => {
        this.operationService.getCarrierList("", null, $("#carrierName_value").val(), "", "", "", "", "", true, 0, 5).then((res) => {
            if (res.data.data.length == 0) {
                this.$ngBootbox.alert('承运商非法,请重新填写');
                return;
            }
            else {
                this.operationService.updateCarrier(this.guid, $("#carrierName_value").val(), this.$scope.CData.carrierId, this.$scope.carrierPhoneBox, this.$scope.carrierTypeBox).then((res) => {
                    this.$ngBootbox.alert("修改成功");
                    $('#CarrierAdd').modal('hide');
                    this.loadData();
                });
            }
        });
    }

    /**
     * 修改车辆信息
     */
    editCarInfo = () => {
        this.$scope.carIdBox = this.$scope.CData.carId;
        this.operationService.getCarDetail(this.$scope.carIdBox).then((res) => {
            let driver;
            if (res.data.driver == null) {
                driver = "";
            }
            else {
                driver = res.data.driver;
            }
            var car = res.data.carCode + " " + driver + " " + res.data.driverPhone;
            $("#car_value").val(car);

        });
        
        
        this.$scope.permitNumberBox = this.$scope.CData.permitNumber;
        this.$scope.countrySubdivisionCode = this.$scope.CData.countrySubdivisionCode;
        this.$scope.roadTransportCertificateNumberBox = this.$scope.CData.roadTransportCertificateNumber;

        this.$scope.countrySubdivisionProvinceCode = this.$scope.CData.countrySubdivisionProvinceCode;
        this.$scope.countrySubdivisionCityCode = this.$scope.CData.countrySubdivisionCityCode;
        this.$scope.countrySubdivisionCountryCode = this.$scope.CData.countrySubdivisionCountryCode;
        this.getProvinceList();
        this.$scope.carCodeBox = this.$scope.CData.carCode;
        this.$scope.driverNameBox = this.$scope.CData.driver;
        this.$scope.driverPhoneBox = this.$scope.CData.driverPhone;
        this.$scope.carTypeBox = this.$scope.CData.vehicleTypeEnum;
        this.$scope.carLengthBox = this.$scope.CData.carLengthEnum;
        this.$scope.carTypeList = this.valueService.getCarTypeList().data;
        this.$scope.carLengthList = this.valueService.getCarLengthList().data;
        this.$scope.searchCar = this.searchCar;
        this.$scope.carCodeSelect = this.carCodeSelect;
        this.$scope.saveCarInfo = this.saveCarInfo;
        if (!$("#carForm").valid()) { return };
        $('#operDetail_driverNameBox').removeClass('error');
        $('#operDetail_carTypeBox').removeClass('error');
        $('#operDetail_carLengthBox').removeClass('error');
    }

    /**
     * 自动填充承运商车辆信息
     */
    searchCar = (value: string) => {
        return this.carService.keywordlist(value, 0, 5).then((res) => {
            return res.data.data;
        });
    }

    carCodeSelect = (select: angucomplete<CarListItemResponse>) => {
        if (select) {
            this.$scope.carIdBox = select.description.id;
            this.$scope.carCodeBox = select.description.carCode;
            this.$scope.driverPhoneBox = select.description.driverPhone;
            this.$scope.driverNameBox = select.description.driver;
            this.operationService.getCarDetail(select.description.id).then((res) => {
                this.$scope.carLengthBox = String(res.data.carLengthInt);
                this.$scope.carTypeBox = String(res.data.carTypeInt);
                this.$scope.permitNumberBox = res.data.permitNumber;
                this.$scope.countrySubdivisionCode = res.data.countrySubdivisionCode;

                let countrySubdinvisionCode = this.$scope.countrySubdivisionCode;
                if (this.$scope.countrySubdivisionCode != undefined || this.$scope.countrySubdivisionCode != null) {
                    this.$scope.countrySubdivisionProvinceCode = countrySubdinvisionCode.substring(0, 2);
                    this.$scope.countrySubdivisionCityCode = countrySubdinvisionCode.substring(0, 4);
                    this.$scope.countrySubdivisionCountryCode = countrySubdinvisionCode.substring(0, 6);
                    this.getProvinceList();
                } else {
                    this.$scope.countrySubdivisionProvinceCode = "-1";
                    this.$scope.countrySubdivisionCityCode = "-1";
                    this.$scope.countrySubdivisionCountryCode = "-1";
                }

                this.$scope.roadTransportCertificateNumberBox = res.data.roadTransportCertificateNumber;
                if (!this.$scope.carLengthBox || this.$scope.carLengthBox == "0") {
                    this.$scope.carLengthBox = "";
                }
                if (this.$scope.carTypeBox == "0" || !this.$scope.carTypeBox) {
                    this.$scope.carTypeBox = "";
                }
            });
        }
    }

    /**
     * 保存修改车辆
     */
    saveCarInfo = () => {
        if (!$("#carForm").valid()) { return };
        this.operationService.getCarList("", null, this.$scope.carCodeBox, "", "", "", "", "", "", "", true, 0, 5).then((res) => {
            
            if (res.data.data.length == 0 || this.$scope.carCodeBox==undefined){
                this.$ngBootbox.alert('车牌号非法,请重新填写');
                return;
            }
            // if(res.data.data.length == 0 || this.$scope.driverPhoneBox==undefined){
            //     this.$ngBootbox.alert('驾驶员号码非法，请重新填写');
            //     return;
            // }
            // else {
                this.operationService.updateCar(this.guid, this.$scope.carIdBox, this.$scope.carCodeBox.toUpperCase(), this.$scope.driverNameBox, this.$scope.driverPhoneBox, this.$scope.carLengthBox, this.$scope.carTypeBox, this.$scope.permitNumberBox, this.$scope.countrySubdivisionProvinceCode, this.$scope.countrySubdivisionCityCode, this.$scope.countrySubdivisionCountryCode, this.$scope.roadTransportCertificateNumberBox).then((res) => {
                    this.$ngBootbox.alert("修改成功");
                    $('#CarsAdd').modal('hide');
                    this.loadData();
                });
            // }
        });
    }

    /**
     * 修改应付价格信息
     */
    editReceivable = () => {
        if (!$("#receiptForm").valid()) { return; }
        $('#operDetail_openBankBox').removeClass('error');
        $('#operDetail_holderBox').removeClass('error');
        this.$scope.openBankDis = true;
        if (this.$scope.CData.advancePaymentList.length == 0) {
            this.$scope.advancePriceBox = ""
            this.$scope.advancePriceTypeBox = ""
            this.$scope.advancePriceTwoBox = ""
            this.$scope.advancePriceTypeTwoBox = ""
        } else {
            if (this.$scope.CData.advancePaymentList.length == 1) {
                this.$scope.advancePriceBox = this.$scope.CData.advancePaymentList[0].advancePrice
                this.$scope.advancePriceTypeBox = this.$scope.CData.advancePaymentList[0].advancePriceType
                this.$scope.advancePriceTwoBox = ""
                this.$scope.advancePriceTypeTwoBox = ""
            } else {
                this.$scope.advancePriceBox = this.$scope.CData.advancePaymentList[0].advancePrice
                this.$scope.advancePriceTypeBox = this.$scope.CData.advancePaymentList[0].advancePriceType
                this.$scope.advancePriceTwoBox = this.$scope.CData.advancePaymentList[1].advancePrice
                this.$scope.advancePriceTypeTwoBox = this.$scope.CData.advancePaymentList[1].advancePriceType
            }
        }
        this.$scope.payablePriceBox = this.$scope.CData.payablePrice;
        this.$scope.payablePriceUnitBox = this.$scope.CData.payablePriceUnitEnum;
        this.$scope.payableSummaryBox = this.$scope.CData.payableSummary;
        this.$scope.receiptPriceBox = this.$scope.CData.receiptPrice;
        this.$scope.receiptPriceUnitBox = this.$scope.CData.receiptPriceUnitEnum;
        $("#bankCode_value").val(this.$scope.CData.bankCode);
        this.$scope.bankCode = this.$scope.CData.bankCode;
        this.$scope.bankNameBox = this.$scope.CData.bankName;
        this.$scope.holderBox = this.$scope.CData.holder;
        let getPriceUnitList = this.valueService.getPriceUnitList().data;
        getPriceUnitList.splice(this.valueService.getPriceUnitList().data.length - 2, 1);
        this.$scope.receivablePriceUnitDropDown = getPriceUnitList;
        this.$scope.advancePriceTypeDropDown = this.valueService.getPaymentMethodList().data;

        this.$scope.searchBank = this.searchBank;
        this.$scope.bankCodeSelect = this.bankCodeSelect;
        this.$scope.getBankName = this.commonService.debounce(this.getBankName, 500);

        this.$scope.saveReceivable = this.saveReceivable;
    }

    /**
     * 银行卡自动填充
     */
    searchBank = (value: string) => {
        return this.operationService.getBankList("", this.$scope.CData.carrierId, value, false, 0, 5).then((rel) => {
            return rel.data.data;
        });
    }

    bankCodeSelect = (select: angucomplete<BankListItemResponse>) => {
        if (select) {
            this.$scope.holderBox = select.description.holder;
            this.$scope.bankNameBox = select.description.bank;
            this.$scope.openBankDis = true;
        }
    }

    getBankName = () => {
        var bankLen = $("#bankCode_value").val().length;
        if (bankLen > 15) {
            this.operationService.BankInfo($("#bankCode_value").val()).then((res) => {
                if (res == null) {
                    res = undefined;
                    this.$scope.openBankDis = false;
                };
                               
                this.$scope.bankNameBox = res.data.extData;
                this.$scope.openBankDis = true;
            }, (rej) => {
                this.$scope.openBankDis = false;
                return;
            });
        }
    }

    /**
     * 保存修改应付价格
     */
    saveReceivable = () => {
        var bankLen = $("#bankCode_value").val().length;
        if (bankLen == "" || bankLen == undefined || bankLen == null || (bankLen > 0) && (bankLen <= 15)) {
            this.$ngBootbox.alert("请输入正确的银行卡号！");
            return;
        }
        if (!$("#receiptForm").valid()) { return; }
        this.AdvancePaymentList = [
            { advancePrice: Number(this.$scope.advancePriceBox), advancePriceType: this.$scope.advancePriceTypeBox },
            { advancePrice: Number(this.$scope.advancePriceTwoBox), advancePriceType: this.$scope.advancePriceTypeTwoBox }];
        if (this.$scope.receiptPriceUnitBox == null) {
            this.$scope.receiptPriceUnitBox = "0";
        }
        this.operationService.updatePayable(this.guid, this.$scope.payablePriceBox, this.$scope.payableSummaryBox, this.$scope.receiptPriceBox, $.trim($("#bankCode_value").val()),
            this.$scope.bankNameBox, this.$scope.holderBox, this.$scope.payablePriceUnitBox, this.$scope.receiptPriceUnitBox, this.AdvancePaymentList).then((res) => {
                this.$ngBootbox.alert("修改成功");
                $('#ReceiptPriceEdit').modal('hide');
                this.loadData();
            });
    }

    /**
     * 修改合同信息
     */
    editContract = () => {
        this.$scope.contractNumberBox = this.$scope.contractData.contractNumber;
        this.$scope.quantityOfGoodsBox = this.$scope.contractData.quantityOfGoods;
        this.$scope.goodsUnitBox = this.$scope.contractData.goodsUnitEnum;
        this.$scope.oilCardNumberBox = this.$scope.contractData.oilCardNumber;
        this.$scope.oilCardStatusBox = this.$scope.contractData.oilCardStatusEnum;
        this.$scope.driverLicenseNumberBox = this.$scope.contractData.driverLicenseNumber;
        this.$scope.frameNumberBox = this.$scope.contractData.frameNumber;
        this.$scope.trailerNumberBox = this.$scope.contractData.trailerNumber;
        this.$scope.engineNumberBox = this.$scope.contractData.engineNumber;
        this.$scope.altermatePhoneBox = this.$scope.contractData.altermatePhone;
        this.$scope.auxiliaryToolBox = this.$scope.contractData.auxiliaryTool;
        this.$scope.trailerFrameNumberBox = this.$scope.contractData.trailerFrameNumber;
        this.$scope.totalPriceBox = this.$scope.contractData.totalPrice;
        this.$scope.goodsNumUnitList = this.valueService.getGoodQuantityUnitList().data;
        this.$scope.oilCardStatusList = this.valueService.getOilCCardStatusList().data;
        this.$scope.saveContract = this.saveContract;
        //备注
        this.$scope.modalRemark = this.$scope.remark;
    }

    /**
     * 保存合同信息
     */
    saveContract = () => {
        this.operationService.updateContract(this.guid, this.$scope.contractData.id, this.$scope.contractNumberBox, this.$scope.quantityOfGoodsBox, this.$scope.goodsUnitBox, this.$scope.oilCardNumberBox,
            this.$scope.oilCardStatusBox, this.$scope.auxiliaryToolBox, this.$scope.engineNumberBox, this.$scope.frameNumberBox, this.$scope.trailerNumberBox, this.$scope.trailerFrameNumberBox, this.$scope.driverLicenseNumberBox,
             this.$scope.altermatePhoneBox,this.$scope.totalPriceBox,this.$scope.modalRemark).then((res) => {
                this.$ngBootbox.alert("修改成功");
                $("#ContractEdit").modal('hide');
                this.loadData();
            })
    }

    /**
     * 修改附件信息
     */
    EditAttachment = () => {
        //清空附件
        $("input[type=file]").val("");
        this.$scope.attachList2.forEach(attach2=>{
            attach2.uploader.clearQueue()
        })
        let carArr2 = this.$scope.CData.carrierOrderAttachments;
        carArr2.forEach((item, index) => {
            this.$scope.attachList2[Number(item.attachmentTag)-1].uploader.addByAttach(item)
           
        });
        this.$scope.cancelSaveAttachment = this.cancelSaveAttachment;
        this.$scope.saveAttachment = this.saveAttachment;
    }

    /**
     * 取消修改附件
     */
    cancelSaveAttachment = () => {
        this.uploaderList = this.attachList;
        this.$scope.attachList2.forEach(attach2=>{
            attach2.uploader.clearQueue();
        })
    }
    /**
     * 保存附件信息
     */
    saveAttachment = () => {
        var attachmentArr = [];
        this.uploaderList.forEach((itemA, indexA) => {
            if (itemA.attachmentTag == "") {
                return;
            }
            else {
                attachmentArr.push(["", itemA.attachmentName, itemA.attachmentPath, itemA.attachmentTag]);
            }
        });
        this.operationService.updateAttachment(this.guid, attachmentArr).then((res) => {
            this.$ngBootbox.alert("修改成功");
            $("#UploadFileEdit").modal('hide');
            this.loadData();
        });
    }

    /**
     * 返回事件
     */
    backUrl = () => {
        this.$state.go("app.opera.operation_manage",{name:'operationManage'});
    }


    /**
     * 选择上传文件后
     */
    onSuccessItem = (fileName: string, filePath: string, tag: number, file?: any) => {
        file.fileId = filePath;
        let attachment = {
            attachmentId: "",
            attachmentName: fileName,
            attachmentPath: filePath,
            attachmentTag: tag.toString()
        };
        this.uploaderList.push(attachment);
    }

    
}

angular.module('tms').controller('operationDetailController', ['$scope', 'orderService', '$location', 'routeService', '$ngBootbox', '$http', 'operationService', 'authorizationService', 'feeService', 'goodsTypeService', 'goodsService', 'valueService', '$state', 'carService', 'commonService', '$timeout', 'areaService','ossUploaderService', OperationDetailController])